#!/usr/bin/env bash

JAVA_CMD="java"
CPATH="./:lib/*:./:services/*"
ID="1"
if [ "x$2" != "x" ]
then 
	ID="$2";
fi

CFG_PATH="conf/"
APPNAME=`cat "${CFG_PATH}application.jdapp"`
PIDFILE="${CFG_PATH}jdapp-daemon${ID}.pid"
DUBBO_CFG="dubbo.properties.file=${CFG_PATH}dubbo.properties"

case $1 in
start)
    echo "Starting Server ${APPNAME}@${ID}..."
	if [ -f "$PIDFILE" ]; then
      if kill -0 `cat "$PIDFILE"` > /dev/null 2>&1; then
         echo $command already running as process `cat "$PIDFILE"`. 
         exit 0
      fi
    fi
	${JAVA_CMD} -cp ${CPATH} -server -D${DUBBO_CFG} cn.zhucongqi.jdapp.server.Main ${ID} &
	;;
stop)
	echo "Stoping Server ${APPNAME}@${ID}..."
	if [ ! -f "$PIDFILE" ]
    then
      echo "no server to stop (could not find file $PIDFILE)"
    else
		kill `cat "$PIDFILE"`
		rm "$PIDFILE"
		echo "The Server ${APPNAME}@${ID} stoped!"
	fi
	exit 0
	;;
*)
    echo "Usage: $0 {start|stop} [server_id=1] [debug|release]" 
    ;;
esac
